package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserPass;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UserPassDaoImpl extends TempDataObjectDao<UserPass> {

    public UserPass getUserPass(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_pass where userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setLong(1, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                UserPass data = parse(rs);
                return data;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    @Override
    protected boolean addData(UserPass data) {
        PreparedStatement call = null;
        String sql = "INSERT INTO t_u_pass(userId,passId,passProgress,loseBlood,modifyTime,buyBuffTimes,itemBuyBuffTimes) VALUES(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getPassId());
            call.setInt(i++, data.getPassProgress());
            call.setString(i++, data.getLoseBlood().toString());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getBuyBuffTimes());
            call.setInt(i++, data.getItemBuyBuffTimes());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected boolean updateData(UserPass data) {
        PreparedStatement call = null;
        String sql = "UPDATE t_u_pass SET passId=?,passProgress=?,loseBlood=?,modifyTime=?,buyBuffTimes=?,itemBuyBuffTimes=? WHERE userId=?";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareStatement(sql);
            int i = 1;
            call.setInt(i++, data.getPassId());
            call.setInt(i++, data.getPassProgress());
            call.setString(i++, data.getLoseBlood().toString());
            call.setLong(i++, data.getModifyTime());
            call.setInt(i++, data.getBuyBuffTimes());
            call.setInt(i++, data.getItemBuyBuffTimes());
            call.setLong(i++, data.getUserId());
            return call.executeUpdate() > 0;
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, null);
        }
        return false;
    }

    @Override
    protected UserPass parseData(ResultSet rs) throws SQLException {
        UserPass data = new UserPass();
        data.setUserId(rs.getLong("userId"));
        data.setPassId(rs.getInt("passId"));
        data.setPassProgress(rs.getInt("passProgress"));
        String loseBlood = rs.getString("loseBlood");
        if (!StringUtils.isNullOrEmpty(loseBlood)) {
            data.setLoseBlood(new BigInteger(loseBlood));
        }
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setBuyBuffTimes(rs.getInt("buyBuffTimes"));
        data.setItemBuyBuffTimes(rs.getInt("itemBuyBuffTimes"));
        return data;
    }

    public boolean checkDbRight(UserPass data) {
        String sql = "REPLACE INTO t_u_pass(userId,passId,passProgress,loseBlood,modifyTime,buyBuffTimes,itemBuyBuffTimes) VALUES(?,?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            List<Map<Integer, DbParameter>> mapList = new ArrayList<>();
            int i = 1;
            Map<Integer, DbParameter> para = new HashMap<>();
            para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
            para.put(i++, new DbParameter(Types.INTEGER,data.getPassId()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getPassProgress()));
            para.put(i++, new DbParameter(Types.NVARCHAR, data.getLoseBlood().toString()));
            para.put(i++, new DbParameter(Types.BIGINT, data.getModifyTime()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getBuyBuffTimes()));
            para.put(i++, new DbParameter(Types.INTEGER, data.getItemBuyBuffTimes()));
            mapList.add(para);
            if (getDb().executeBatchQuery(sql, mapList) > -1) {
                return true;
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        }
        return false;
    }
}
